package com.tuyrt.firstcode.kotlin.basic

/**
 * Created by tuyrt7 on 2021/4/6.
 * 语句： if,when,while,for
 */

fun main() {
    var a = 1
    var b = 2

    //if 取较大值
    val ifValue = if (a > b) a else b

    val score = getScore("Jack")
    val car = getCarType(Car.RedCar)
}

// when 表达式
fun getScore(name: String) = when (name) {
    "Tom" -> 86
    "Jack" -> 73
    else -> 59
}

fun getCarType(car: Car) = when (car) {
    is Car.RedCar -> "is red."
    is Car.GreenCar -> "is green."
}


fun loop() {
    //1 2 3 .. 10
    for (i in 1..10) {
        println(i)
    }

    //0 1 .. 9
    for (i in 0 until 10) {
        println(i)
    }

    //0 2 .. 10
    for (i in 0 until 10 step 2) {
        println(i)
    }

    // 10 9... 1
    for (i in 10 downTo 1) {
        println(i)
    }
}